{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4723b828-103f-4d7d-896e-c49a50c81a48",
   "metadata": {},
   "source": [
    "# Interactive Panel App with `bind`\n",
    "\n",
    "An interactive app that filters penguins by species and body mass threshold using [Panel](https://panel.holoviz.org) widgets and setting up interactivity with [`pn.bind`](https://panel.holoviz.org/tutorials/basic/pn_bind.html). The filtered data is visualized as a scatter plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0af69e5f-776b-4f8a-9bce-35418dea1375",
   "metadata": {},
   "outputs": [],
   "source": [
    "import panel as pn\n",
    "import hvplot.pandas  # noqa\n",
    "\n",
    "df = hvplot.sampledata.penguins(\"pandas\").dropna()\n",
    "\n",
    "species_widget = pn.widgets.Select(\n",
    "    name='Species',\n",
    "    options=list(df['species'].unique()),\n",
    "    width=200,\n",
    ")\n",
    "\n",
    "mass_widget = pn.widgets.IntSlider(\n",
    "    name='Min Body Mass (g)',\n",
    "    start=int(df['body_mass_g'].min()),\n",
    "    end=int(df['body_mass_g'].max()),\n",
    "    step=100,\n",
    "    value=4000,\n",
    "    width=200,\n",
    ")\n",
    "\n",
    "def plot_penguins(species, min_mass):\n",
    "    subset = df[(df['species'] == species) & (df['body_mass_g'] >= min_mass)]\n",
    "    return subset.hvplot.scatter(\n",
    "        x='bill_length_mm',\n",
    "        y='bill_depth_mm',\n",
    "        color='body_mass_g',\n",
    "        cmap='viridis_r',\n",
    "        size=24,\n",
    "        title=f'{species} Penguins (body mass ≥ {min_mass})',\n",
    "        xlabel='Bill Length (mm)',\n",
    "        ylabel='Bill Depth (mm)',\n",
    "    )\n",
    "\n",
    "plot = pn.bind(plot_penguins, species=species_widget, min_mass=mass_widget)\n",
    "\n",
    "pn.Column(pn.Row(species_widget, mass_widget), plot)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e51bcd1e-42f3-4e5d-9acf-8c08629eff82",
   "metadata": {},
   "source": [
    ":::{seealso}\n",
    "- [Scatter Plots reference documentation](../../ref/api/manual/hvplot.hvPlot.scatter.ipynb)\n",
    "- [Panel Component Gallery](https://panel.holoviz.org/reference/index.html)\n",
    "- [Panel Interactivity How-To Guides](https://panel.holoviz.org/how_to/interactivity/index.html)\n",
    ":::"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
